package edu.fit5046.mymonashmate.activity;


import java.util.concurrent.ExecutionException;

import org.json.JSONException;
import org.json.JSONObject;

import edu.fit5046.mymonashmate.R;
import edu.fit5046.mymonashmate.utils.DetailInfo;
import edu.fit5046.mymonashmate.utils.HttpUtils;
import edu.fit5046.mymonashmate.utils.SimpleInfo;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

public class InfoActivity extends Activity{
	public static final String TAG ="Info";
	public static final int INFO_END = DetailInfo.INFO_PREVIOUSJOB + 1;
	public static final String url = "/entity.mymonashmates";
	private EditText editTextRegInfo[];
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.layout_info);
		initWidget();
		
		//getValue from mapActivity
		Bundle bundle = this.getIntent().getExtras();
		int stuID = bundle.getInt("stuID");
		
		//get info form networ
		DetailInfo info = null;
		try {
			info = getInfoFromNetwork(stuID);
		} catch (JSONException | InterruptedException | ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		updateView(info);
		
		super.onCreate(savedInstanceState);
	}
	
	private void initWidget(){
		editTextRegInfo = new EditText[INFO_END];
		editTextRegInfo[DetailInfo.INFO_NAME] = (EditText) findViewById(R.id.text_Name);
		editTextRegInfo[DetailInfo.INFO_SURNAME] = (EditText) findViewById(R.id.text_SurName);
		editTextRegInfo[DetailInfo.INFO_COURSE] = (EditText) findViewById(R.id.text_Course);
		editTextRegInfo[DetailInfo.INFO_UNIT] = (EditText) findViewById(R.id.text_Unit);
		
		editTextRegInfo[DetailInfo.INFO_NATIONNANITY] = (EditText) findViewById(R.id.text_NNanity);
		editTextRegInfo[DetailInfo.INFO_FIRSTLANGUAGE] = (EditText) findViewById(R.id.text_FLanguage);
		editTextRegInfo[DetailInfo.INFO_SECONDLANGUAGE] = (EditText) findViewById(R.id.text_SLanguage);
		editTextRegInfo[DetailInfo.INFO_ADDRESS] = (EditText) findViewById(R.id.text_address);
		editTextRegInfo[DetailInfo.INFO_FAVOURITEFOOD] = (EditText) findViewById(R.id.text_FFood);
		editTextRegInfo[DetailInfo.INFO_FAVOURITEMOIVE] = (EditText) findViewById(R.id.text_FMovie);
		editTextRegInfo[DetailInfo.INFO_FAVOURITEUNIT] = (EditText) findViewById(R.id.text_FUnit);
		editTextRegInfo[DetailInfo.INFO_FAVOURITEPROLAN] = (EditText) findViewById(R.id.text_FProLanguage);
		editTextRegInfo[DetailInfo.INFO_CURRENTJOB] = (EditText) findViewById(R.id.text_CJob);
		editTextRegInfo[DetailInfo.INFO_PREVIOUSJOB] = (EditText) findViewById(R.id.text_PJob);
		
		CheckBox box0 = (CheckBox) findViewById(R.id.box0);
		box0.setVisibility(View.GONE);
		CheckBox box1 = (CheckBox) findViewById(R.id.box1);
		box1.setVisibility(View.GONE);
		CheckBox box2 = (CheckBox) findViewById(R.id.box2);
		box2.setVisibility(View.GONE);
		CheckBox box3 = (CheckBox) findViewById(R.id.box3);
		box3.setVisibility(View.GONE);
		CheckBox box4 = (CheckBox) findViewById(R.id.box4);
		box4.setVisibility(View.GONE);
		CheckBox box5 = (CheckBox) findViewById(R.id.box5);
		box5.setVisibility(View.GONE);
		CheckBox box6 = (CheckBox) findViewById(R.id.box6);
		box6.setVisibility(View.GONE);
		CheckBox box7 = (CheckBox) findViewById(R.id.box7);
		box7.setVisibility(View.GONE);
		CheckBox box8 = (CheckBox) findViewById(R.id.box8);
		box8.setVisibility(View.GONE);
		CheckBox box9 = (CheckBox) findViewById(R.id.box9);
		box9.setVisibility(View.GONE);
		
		
		for (int i = 0; i < INFO_END; i++) {
			editTextRegInfo[i].setEnabled(false);
		}
		
		Button courseButton = (Button) findViewById(R.id.button_Course);
		courseButton.setVisibility(View.GONE);
		Button unitButton = (Button) findViewById(R.id.button_Unit);
		unitButton.setVisibility(View.GONE);
		Button okButton = (Button) findViewById(R.id.up_ok);
		okButton.setVisibility(View.GONE);
		Button cancelbutton = (Button) findViewById(R.id.up_cancel);
		cancelbutton.setVisibility(View.GONE);
	}
	
	private void updateView(DetailInfo info){
		for(int i = 0; i < INFO_END; i++){
			editTextRegInfo[i].setText(info.getValue(i));
		}
	}
	
	//when invoke this method, try/catch must be used
	@SuppressLint("NewApi")
	private DetailInfo getInfoFromNetwork(int stuID) throws JSONException, InterruptedException, ExecutionException{
		String jsObj = new MyTask().execute(url + "/get/"+stuID).get();
		String i = "1Nvi5IblE";
		if(stuID==SimpleInfo.localId){
			jsObj = jsObj.replace(i, "");
		}
		DetailInfo info = new DetailInfo(jsObj);
		
		JSONObject js = new JSONObject(jsObj);

	
		info.ID = stuID;
		//cannot be null 
		info.name=js.getString("givenname");		
		info.surName=js.getString("surname");
		info.course = js.getString("course");
		info.unit=js.getString("unit");
		//location
		info.latitude=js.getDouble("latitude");
		info.longitude=js.getDouble("longitude");
		//hobbies & interests

		info.favouriteFood=(!js.getString("foodlike").isEmpty()&&!js.getString("foodlike").contains(i))?js.getString("foodlike"):"...";
		info.favouriteMovie=(!js.getString("movielike").isEmpty()&&!js.getString("movielike").contains(i))?js.getString("movielike"):"...";
		info.favouriteProgremLanguage=(!js.getString("pglanglike").isEmpty()&&!js.getString("pglanglike").contains(i))?js.getString("pglanglike"):"...";
		info.favouriteUnit=(!js.getString("unitlike").isEmpty()&&!js.getString("unitlike").contains(i))?js.getString("unitlike"):"...";
		info.nationNanity=(!js.getString("nationality").isEmpty()&&!js.getString("nationality").contains(i))?js.getString("nationality"):"...";
		info.firstLanguage=(!js.getString("fstlang").isEmpty()&&!js.getString("fstlang").contains(i))?js.getString("fstlang"):"...";
		info.secondLanguage=(!js.getString("sndlang").isEmpty()&&!js.getString("sndlang").contains(i))?js.getString("sndlang"):"...";
		info.address =(!js.getString("suburb").isEmpty()&&!js.getString("suburb").contains(i))?js.getString("suburb"):"...";
		info.currentJob=(!js.getString("job").isEmpty()&&!js.getString("job").contains(i))?js.getString("job"):"...";
		info.previousJob=(!js.getString("prejob").isEmpty()&&!js.getString("prejob").contains(i))?js.getString("prejob"):"...";
		//17 attributes in total
		
		//return info
		return info;
	}
	
	private class MyTask extends AsyncTask<String, String, String> {
		@Override
		protected String doInBackground(String... prm) {
			// TODO Auto-generated method stub

			return HttpUtils.Get(prm[0]);
		}

		protected void onPostExecute(String result) {
			super.onPostExecute(result);
		}

	}

}
